package io.renren.modules.pms.controller;

import com.alibaba.excel.EasyExcel;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.pms.dto.GoodsStockRecordDTO;
import io.renren.modules.pms.entity.GoodsStockRecordEntity;
import io.renren.modules.pms.service.GoodsInfoService;
import io.renren.modules.pms.service.GoodsStockRecordService;
import io.renren.modules.pms.vo.GoodsStockRecordVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Map;



/**
 * 商品出入库记录表

 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2024-08-13 16:03:19
 */
@RestController
@RequestMapping("pms/goodsstockrecord")
public class GoodsStockRecordController {
    @Autowired
    private GoodsStockRecordService goodsStockRecordService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("pms:goodsstockrecord:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsStockRecordService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("pms:goodsstockrecord:info")
    public R info(@PathVariable("id") Integer id){
        GoodsStockRecordEntity goodsStockRecord = goodsStockRecordService.getById(id);

        return R.ok().put("goodsStockRecord", goodsStockRecord);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("pms:goodsstockrecord:save")
    public R save(@RequestBody GoodsStockRecordDTO goodsStockRecordDTO){
        ValidatorUtils.validateEntity(goodsStockRecordDTO);

        goodsStockRecordService.saveGoodsStockRecord(goodsStockRecordDTO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("pms:goodsstockrecord:update")
    public R update(@RequestBody GoodsStockRecordDTO goodsStockRecordDTO){
        ValidatorUtils.validateEntity(goodsStockRecordDTO);

        goodsStockRecordService.updateGoodsStockRecord(goodsStockRecordDTO);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("pms:goodsstockrecord:delete")
    public R delete(@RequestBody Integer[] ids){
        goodsStockRecordService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 下载进货明细
     **/
    @GetMapping("/download")
    @RequiresPermissions("pms:goodsstockrecord:download")
    public void download(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("进货明细", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), GoodsStockRecordVo.class).sheet("模板").doWrite(goodsStockRecordService.getPurchaseDetails());
    }
}
